/******************************************************************************

Copyright(c) 2005~2010 SunMedia technology (Chengdu) co. Ltd.All right reserved!

 ******************************************************************************
  File Name     : auto_config_h.c
  Version       : Initial Draft
  Author        : sjhuang
  Created       : 2010/3/25
  Last Modified :
  Description   : Generate auto_config from .config (make menuconfig)
  Function List :
                   get_define_string(char*,char*)
                   main(int,char)

 History Information Description
 Date                         Author                         Modification
 2010/3/25                    sjhuang                        Created function

******************************************************************************/

/*---------------------------------------------------------------------------*
 *                            INCLUDE   DECLARATIONS                         *
 *---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*---------------------------------------------------------------------------*
 *                            EXTERNAL   REFERENCES                          *
 *---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*
 *                            MACRO   DECLARATIONS                           *
 *---------------------------------------------------------------------------*/
//#define NO_NEED_CONFIG_FREFIX
#define CONFIG_FREFIX "CONFIG_"
#define CONFIG_COMMENT_FLAG "#"
#define CONFIG_EQUAL_FLAG "="
#define CONFIG_YES_FLAG "=y"
#define CONFIG_STRING_FLAG "=\""
#define LINE_MAX 256

/*---------------------------------------------------------------------------*
 *                            GLOBAL   VARIABLES                             *
 *---------------------------------------------------------------------------*/
char Configuration_file[256]="auto_config.h";

/*---------------------------------------------------------------------------*
 *                          FUNCTION   DECLARATIONS                          *
 *---------------------------------------------------------------------------*/
static int  get_define_string(char* pToolName, char* pFileName)
{
#ifdef NO_NEED_CONFIG_FREFIX
	const int uiConfigPrefixLen = strlen(CONFIG_FREFIX);
#endif
	char line[LINE_MAX];
	char line2[LINE_MAX];
	char *pDefineStart;
	FILE *pFileConfig;
	FILE *pFile = fopen(pFileName, "r");
	time_t now;

	if (NULL == pFile)
	{
		printf("Cannot open file \"%s\" for reading!\n", pFileName);
		exit(-1);
	}
	pFileConfig = fopen(Configuration_file, "w");
	//time(&now);
	//fprintf(pFileConfig, "/*\n * Automatically generated by %s don't edit \n *%s */\n\n", pToolName, ctime(&now));
	fprintf(pFileConfig, "/*\n * Automatically generated by %s. Please don't edit.  */\n\n", pToolName);
	fprintf(pFileConfig, "#ifndef __AUTO_CONFIGURATION_H__\n");
	fprintf(pFileConfig, "#define __AUTO_CONFIGURATION_H__\n\n\n");

	fprintf(pFileConfig, "#include <auto_config.inc>\n\n");

	while (NULL != fgets(line, LINE_MAX, pFile))
	{
		if (NULL != strstr(line,CONFIG_COMMENT_FLAG))
		{
			//4 comment current skip only
			continue;
		}
		line2[0] = 0;
		if (NULL != (pDefineStart = strstr(line, CONFIG_YES_FLAG)))
		{
#ifdef NO_NEED_CONFIG_FREFIX
			strncat(line2, line + uiConfigPrefixLen, pDefineStart - (line + uiConfigPrefixLen));
#else
			strncat(line2, line, pDefineStart - line);
#endif
			fprintf(pFileConfig, "#define %s \t 1\n", line2);
		}
		else if (NULL != (pDefineStart = strstr(line, CONFIG_EQUAL_FLAG)))
		{
#ifdef NO_NEED_CONFIG_FREFIX
			strncat(line2, line + uiConfigPrefixLen, pDefineStart - (line + uiConfigPrefixLen));
#else
			strncat(line2, line, pDefineStart - line);
#endif
			fprintf(pFileConfig, "#define %s \t  ", line2);
			line2[0] = 0;
			strcpy(line2, pDefineStart + 1);
			fprintf(pFileConfig, "%s",line2);
		}
	}

	fprintf(pFileConfig, "\n#endif\n\n");
	fclose(pFileConfig);
	fclose(pFile);
	return 0;
}


int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		printf("Usage: %s <infile > \n", argv[0]);
		exit(-1);
	}
	if (argc == 3)
	{
		//printf("Usage: %s <.config > [auto_config.h==>%s ] \n", argv[0], argv[2]);
		strcpy(Configuration_file, argv[2]);
	}
	get_define_string(argv[0],argv[1]);

	return 0;
}

